package com.neuedu.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neuedu.core.CanalMessage;
import com.neuedu.entity.PmsOrder;
import com.neuedu.entity.PmsRegist;
import com.neuedu.service.MedicalInfoService;
import com.neuedu.service.PmsRegistService;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class OrderMessage {
    @Resource
    PmsRegistService pmsRegistService;
    @Resource
    MedicalInfoService medicalInfoService;
    @RabbitListener(queues = "canalOrderQueue")
    public void receive(String message) {

        try {
            CanalMessage canalMessage = JSONObject.parseObject(message, CanalMessage.class);
            if (!"UPDATE".equalsIgnoreCase(canalMessage.getType())) {
                return;
            }
            List<PmsOrder> oldList = JSONObject.parseArray(canalMessage.getOld(), PmsOrder.class);
            PmsOrder oldOrder = oldList.get(0);
            if (null == oldOrder.getActive()) {
                return;
            }
            List<PmsOrder> newList = JSONObject.parseArray(canalMessage.getData(), PmsOrder.class);
            PmsOrder newOrder = newList.get(0);
            String type = newOrder.getType();
            if ("挂号费".equals(type)) {
                updateRegister(newOrder.getNo(), newOrder.getActive());
            } else if ("检查费".equals(type)) {
                updateCheck(newOrder.getNo(), newOrder.getActive());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void updateRegister(String no, byte active) {
        pmsRegistService.updatePay(no, active);
    }
    public void updateCheck(String no, byte active) {
        medicalInfoService.updateCheck(no, active);
    }
}
